package com.example.chirdrenserver.controller.AdminController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.chirdrenserver.Authority.Authority;
import com.example.chirdrenserver.Pojo.dto.BasePage;
import com.example.chirdrenserver.Pojo.dto.admin.CommentDTO;
import com.example.chirdrenserver.Pojo.vo.admin.CommentVO;
import com.example.chirdrenserver.Service.AdminService.AdminCommentService;
import com.example.chirdrenserver.Utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/comment")
public class AdminCommentController {

    @Autowired
    private AdminCommentService adminCommentService;

    @PostMapping
    @Authority(authority= "permission:comment")
    public Result addComment(@RequestBody CommentDTO commentDTO) {
        boolean success = adminCommentService.addComment(commentDTO);
        return success ?
                Result.success("评论添加成功") :
                Result.error("评论添加失败");
    }

    @DeleteMapping("/{id}")
    @Authority(authority= "permission:comment")
    public Result deleteComment(@PathVariable Long id) {
        boolean success = adminCommentService.deleteComment(id);
        return success ?
                Result.success("评论删除成功") :
                Result.error("评论删除失败");
    }

    @GetMapping("/{id}")
    @Authority(authority= "permission:comment")
    public Result<CommentVO> getCommentDetail(@PathVariable Long id) {
        CommentVO commentVO = adminCommentService.getCommentById(id);
        if (commentVO == null) {
            return Result.error("评论不存在");
        }
        return Result.success(commentVO);
    }

    @GetMapping("/list")
    @Authority(authority= "permission:comment")
    public Result<IPage<CommentVO>> getCommentList(
            @RequestParam(required = false) Long postId,
            @RequestParam(required = false) String sortField,
            @RequestParam(required = false) String sortOrder,
            @RequestParam(defaultValue = "1") Long currentPage,
            @RequestParam(defaultValue = "10") Long pageSize) {

        // 处理排序顺序默认值
        String actualSortOrder = (sortOrder == null || sortOrder.isEmpty()) ? "asc" : sortOrder;

        // 使用带参构造函数创建BasePage
        BasePage basePage = new BasePage(
                currentPage.longValue(),  // 转换为Long
                pageSize.longValue() // 转换为Long
        );

        IPage<CommentVO> voPage = adminCommentService.getCommentList(postId, basePage);
        return Result.success(voPage);
    }

    @PostMapping("/reply/{parentId}")
    @Authority(authority= "permission:comment")
    public Result replyComment(@PathVariable Long parentId,
                               @Validated @RequestBody CommentDTO commentDTO) {
        boolean success = adminCommentService.replyComment(parentId, commentDTO);
        return success ?
                Result.success("回复成功") :
                Result.error("回复失败");
    }

    @GetMapping("/replies/{parentId}")
    @Authority(authority= "permission:comment")
    public Result<List<CommentVO>> getCommentReplies(@PathVariable Long parentId) {
        List<CommentVO> voList = adminCommentService.getRepliesByParentId(parentId);
        return Result.success(voList);
    }
}